<?php
namespace app\index\controller;

use think\facade\View;
use app\admin\model\Article as ArticleModel;
use app\admin\model\Category as CategoryModel;

/**
 * 文章控制器
 * @author huwhis@163.com
 * @version   0.0.2
 */
class Article extends Base
{
    public function index($cid = 0)
    {
        if (!$cid) {
            throw new \think\exception\HttpException(404, '页面不存在');
        }

        $category_info = CategoryModel::field('id, pid, name, url, template_list')->find($cid);


        $param = $this->app->request->param();

        $list = ArticleModel::queryPage($param);

        $template =$category_info->template_list ? : 'index';

        return View::fetch($template,[
            'category_info' => $category_info,
            'list' => $list,
            'cid' => $cid,
        ]);
    }

    public function detail($id = 0)
    {
        if (!$id) {
            throw new \think\exception\HttpException(404, '页面不存在');
        }
        
        $data = ArticleModel::field('id, cid, title, clicks, title_pic, writer, source, keywords, summary, content, create_time')->find($id);
        
        if (!$data) {
            throw new \think\exception\HttpException(404, '页面不存在');
        }

        $category_info = CategoryModel::field('id, pid, name, template_detail')->find($data->cid);

        $data->clicks += 1;
        
        $data->save();

        $template = $category_info->template_detail ? : 'detail'; 

        return View::fetch($template, [
            'category_info' => $category_info,
            'data' => $data
        ]);
    }
}
